package com.ruoyi.book.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.book.domain.BookCommission;
import com.ruoyi.book.mapper.BookCommissionUsersMapper;
import com.ruoyi.book.domain.BookCommissionUsers;
import com.ruoyi.book.service.IBookCommissionUsersService;

/**
 * 图书委托用户Service业务层处理
 * 
 * @author hd
 * @date 2025-03-31
 */
@Service
public class BookCommissionUsersServiceImpl implements IBookCommissionUsersService 
{
    @Autowired(required=false)
    private BookCommissionUsersMapper bookCommissionUsersMapper;

    /**
     * 查询图书委托用户
     * 
     * @param userId 图书委托用户主键
     * @return 图书委托用户
     */
    @Override
    public BookCommissionUsers selectBookCommissionUsersByUserId(Long userId)
    {
        return bookCommissionUsersMapper.selectBookCommissionUsersByUserId(userId);
    }

    /**
     * 查询图书委托用户列表
     * 
     * @param bookCommissionUsers 图书委托用户
     * @return 图书委托用户
     */
    @Override
    public List<BookCommissionUsers> selectBookCommissionUsersList(BookCommissionUsers bookCommissionUsers)
    {
        return bookCommissionUsersMapper.selectBookCommissionUsersList(bookCommissionUsers);
    }

    /**
     * 新增图书委托用户
     * 
     * @param bookCommissionUsers 图书委托用户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookCommissionUsers(BookCommissionUsers bookCommissionUsers)
    {
        int rows = bookCommissionUsersMapper.insertBookCommissionUsers(bookCommissionUsers);
        insertBookCommission(bookCommissionUsers);
        return rows;
    }

    /**
     * 修改图书委托用户
     * 
     * @param bookCommissionUsers 图书委托用户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookCommissionUsers(BookCommissionUsers bookCommissionUsers)
    {
        bookCommissionUsersMapper.deleteBookCommissionByUserId(bookCommissionUsers.getUserId());
        insertBookCommission(bookCommissionUsers);
        return bookCommissionUsersMapper.updateBookCommissionUsers(bookCommissionUsers);
    }

    /**
     * 批量删除图书委托用户
     * 
     * @param userIds 需要删除的图书委托用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCommissionUsersByUserIds(Long[] userIds)
    {
        bookCommissionUsersMapper.deleteBookCommissionByUserIds(userIds);
        return bookCommissionUsersMapper.deleteBookCommissionUsersByUserIds(userIds);
    }

    /**
     * 删除图书委托用户信息
     * 
     * @param userId 图书委托用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCommissionUsersByUserId(Long userId)
    {
        bookCommissionUsersMapper.deleteBookCommissionByUserId(userId);
        return bookCommissionUsersMapper.deleteBookCommissionUsersByUserId(userId);
    }

    /**
     * 新增图书委托信息
     * 
     * @param bookCommissionUsers 图书委托用户对象
     */
    public void insertBookCommission(BookCommissionUsers bookCommissionUsers)
    {
        List<BookCommission> bookCommissionList = bookCommissionUsers.getBookCommissionList();
        Long userId = bookCommissionUsers.getUserId();
        if (StringUtils.isNotNull(bookCommissionList))
        {
            List<BookCommission> list = new ArrayList<BookCommission>();
            for (BookCommission bookCommission : bookCommissionList)
            {
                bookCommission.setUserId(String.valueOf(userId));
                list.add(bookCommission);
            }
            if (list.size() > 0)
            {
                bookCommissionUsersMapper.batchBookCommission(list);
            }
        }
    }
}
